#pragma once
#include <cstdio>
#include <cassert>
#include <cstring>
#include <iostream>
#include <unistd.h>
#include <fcntl.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/epoll.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#define SIZE(a) (sizeof(a) / sizeof(a[0]))
typedef void (*sighandler_t)(int);
#define ARGS_CHECK(argc, num)                 \
    {                                         \
        if (argc != num)                      \
        {                                     \
            fprintf(stderr, "ARGS ERROR!\n"); \
            return -1;                        \
        }                                     \
    }
#define ERROR_CHECK(ret, num, msg) \
    {                              \
        if (ret == num)            \
        {                          \
            perror(msg);           \
            return -1;             \
        }                          \
    }
#define THREAD_ERROR_CHECK(ret, func)                        \
    {                                                        \
        if (ret != 0)                                        \
        {                                                    \
            fprintf(stderr, "%s:%s\n", func, strerror(ret)); \
        }                                                    \
    }
struct train_t
{
    int len;
    char buff[1000];
};
int recvn(int sockfd, void *buff, int len)
{
    int left = len;
    int ret = 0;
    char *pbuf = (char *)buff;
    while (left > 0)
    {
        ret = recv(sockfd, pbuf, left, 0);
        if (ret < 0)
        {
            perror("recv");
            return -1;
        }
        else if (ret == 0)
        {
            break;
        }
        pbuf += ret;
        left -= ret;
    }
    return len - left;
}
int sendn(int sockfd, const void *buff, int len)
{
    int left = len;
    int ret = 0;
    const char *pbuf = (const char *)buff;
    while (left > 0)
    {
        ret = send(sockfd, pbuf, left, 0);
        if (ret == -1)
        {
            perror("send");
            return -1;
        }
        pbuf += ret;
        left -= ret;
    }
    return len - left;
}